//
//  YYDeviceConnectedView.m
//  Project
//
//  Created by Yown on 2024/1/25.
//

#import "YYDeviceConnectedView.h"
#import "YYHomeBatteryView.h"

#import <BIBluetoothModule/BIBluetoothManager.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIProjectModule/BIAppDefine.h>

#import <SDWebImage/SDWebImage.h>
#import <Masonry/Masonry.h>

@interface YYDeviceConnectedView ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *disconnectButton;

@property (nonatomic) YYHomeBatteryView *recevierBatteryView;
@property (nonatomic) YYHomeBatteryView *micFirstBatteryView;
@property (nonatomic) YYHomeBatteryView *micSecondBatteryView;

@end

@implementation YYDeviceConnectedView

#pragma mark - Life Cycle

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
    [self setupLayout];
}

- (void)setupViews {
    self.nameLabel.textColor = [UIColor loadColorFromString:@"#333333"];
    self.nameLabel.font = BIFont(18);
    
    self.disconnectButton.backgroundColor = [UIColor clearColor];
    self.disconnectButton.layer.masksToBounds = YES;
    self.disconnectButton.layer.cornerRadius = 16;
    self.disconnectButton.layer.borderWidth = 1.0;
    self.disconnectButton.layer.borderColor = BIColor(BIAppMainColor).CGColor;
    self.disconnectButton.titleLabel.font = BIFont(12);
    [self.disconnectButton setTitleColor:BIColor(BIAppMainColor) forState:UIControlStateNormal];
    [self.disconnectButton setTitle:BILocalizedString(@"home_close_device") forState:UIControlStateNormal];
    
    self.recevierBatteryView = [YYHomeBatteryView view];
    self.recevierBatteryView.hidden = YES;
    [self.recevierBatteryView reloadName:@"home_recevier"];
    [self addSubview:self.recevierBatteryView];
    
    self.micFirstBatteryView = [YYHomeBatteryView view];
    self.micFirstBatteryView.hidden = YES;
    [self.micFirstBatteryView reloadName:@"home_mic_first"];
    [self addSubview:self.micFirstBatteryView];
    
    self.micSecondBatteryView = [YYHomeBatteryView view];
    self.micSecondBatteryView.hidden = YES;
    [self.micSecondBatteryView reloadName:@"home_mic_second"];
    [self addSubview:self.micSecondBatteryView];
}

- (void)setupLayout {
    [self.recevierBatteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.disconnectButton.mas_bottom).mas_equalTo(35);
        make.left.mas_equalTo(self.nameLabel.mas_left).mas_equalTo(20);
    }];
    
    [self.micFirstBatteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.disconnectButton.mas_bottom).mas_equalTo(35);
        make.centerX.mas_equalTo(0);
    }];
    
    [self.micSecondBatteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.disconnectButton.mas_bottom).mas_equalTo(35);
        make.right.mas_equalTo(self.disconnectButton.mas_right).mas_equalTo(-20);
    }];
}

- (void)reloadDeviceState:(BIBluetoothState)state {
    if (state == kBIBluetoothConnected) {
        self.hidden = NO;
    } else {
        self.hidden = YES;
    }
}

- (void)reloadDeviceName:(NSString *)name {
    self.nameLabel.text = name;
}

- (void)reloadRecevierBattery:(NSInteger)battery {
    if (battery < 0) {
        self.recevierBatteryView.hidden = YES;
    } else {
        self.recevierBatteryView.hidden = NO;
        [self.recevierBatteryView reloadBattery:battery];
    }
}

- (void)reloadMicFirstBattery:(NSInteger)battery {
    if (battery < 0) {
        self.micFirstBatteryView.hidden = YES;
    } else {
        self.micFirstBatteryView.hidden = NO;
        [self.micFirstBatteryView reloadBattery:battery];
    }
}

- (void)reloadMicSecondBattery:(NSInteger)battery {
    if (battery < 0) {
        self.micSecondBatteryView.hidden = YES;
    } else {
        self.micSecondBatteryView.hidden = NO;
        [self.micSecondBatteryView reloadBattery:battery];
    }
}

- (void)reloadUIWithLanguageChanged {
    [self.disconnectButton setTitle:BILocalizedString(@"home_close_device") forState:UIControlStateNormal];
    [self.recevierBatteryView reloadName:@"home_recevier"];
    [self.micFirstBatteryView reloadName:@"home_mic_first"];
    [self.micSecondBatteryView reloadName:@"home_mic_second"];
}

#pragma mark - Action

- (IBAction)clickDisconnectButton:(UIButton *)sender {
    BISafeBlock(self.clickDisconnectButtonBlock);
    [[BIBluetoothManager sharedInstance] disconnectBluetooth:nil];
}


@end
